/**
 * @author corleone
 * @date 2018/7/12 0012
 */
package com.self.test;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.web3j.crypto.CipherException;
import org.web3j.crypto.ECKeyPair;
import org.web3j.crypto.Wallet;

import java.io.File;
import java.math.BigInteger;
import java.security.*;
import java.security.spec.ECGenParameterSpec;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class MainEntrance2 {

    static KeyPair createSecp256k1KeyPair() throws NoSuchProviderException,
            NoSuchAlgorithmException, InvalidAlgorithmParameterException {

        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("ECDSA", "BC");
        ECGenParameterSpec ecGenParameterSpec = new ECGenParameterSpec("secp256k1");
        keyPairGenerator.initialize(ecGenParameterSpec, new SecureRandom());
        return keyPairGenerator.generateKeyPair();
    }

    public static ECKeyPair createEcKeyPair() throws InvalidAlgorithmParameterException,
            NoSuchAlgorithmException, NoSuchProviderException {
        KeyPair keyPair = createSecp256k1KeyPair();
        return ECKeyPair.create(keyPair);
    }

    private ECKeyPair generatePublicPrivateKey() throws Exception {
        return Key.createEcKeyPair();
    }

    private static String buildAddress(ECKeyPair ecKeyPair) throws Exception {
        return Wallet.createLight("1", ecKeyPair).getAddress();
    }

    private static void buildPublicKeyViaPrivateKey() {

    }

    private static void process() {
        try {
            ECKeyPair ecKeyPair = Key.createEcKeyPair();
//            ECKeyPair ecKeyPair = createEcKeyPair();
            BigInteger privateKeyInDec = ecKeyPair.getPrivateKey();
            BigInteger publicKeyInDec = ecKeyPair.getPublicKey();

            String sPrivatekeyInHex = privateKeyInDec.toString(16);

            String sAddress = Wallet.createLight("1", ecKeyPair).getAddress();

            System.out.println(sPrivatekeyInHex);
            System.out.println(publicKeyInDec);
            System.out.println("0x" + sAddress);
//            21fcef74b41bef4ae788b5cd7da641616ac0f673f46471c12311292781095dd6
//            40715793776480825566193491802091904207269333415565074266223078402873161998386

        } catch (CipherException e) {
            //
        } catch (InvalidAlgorithmParameterException e) {
            //
        } catch (NoSuchAlgorithmException e) {
            //
        } catch (NoSuchProviderException e) {
            //
        }

    }


    public static void main(String[] args) throws Exception {

        MainEntrance2 me = new MainEntrance2();

        while (true){
            long now = System.currentTimeMillis();
            List<String> datas = new ArrayList<>();
            for (int i = 0; i < 1000; i++) {
                ECKeyPair ppk = me.generatePublicPrivateKey();
                String address = buildAddress(ppk);
                datas.add(StringUtils.joinWith("|", address, ppk.getPublicKey().toString(16), ppk.getPrivateKey().toString(16)));
            }
            FileUtils.writeLines(new File("d:/ppkey-data/" + UUID.randomUUID().toString() + ".txt"), "UTF-8", datas);
            System.out.println(System.currentTimeMillis() - now);
        }
    }

}
